<?php

namespace App\Http\Controllers\Admin\qyf;
use App\Common\Responses;
use App\Http\Controllers\Admin\ly\AgentController;
use App\Models\Agent;
use App\Utils\Lib\Redis;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class Agents extends AgentController
{

    //查看经纪人&&详情&&查询
    function lookBroker(Request $request){
       //过滤信息
        $data= $request->except(['id']);
        //多条件搜索
        if (!empty($data)){
            $arr=[];
            foreach ($data as $item=>$value){
              $arr[]=["$item",'like',"%$value%"];
            }
            try {
                $data= Agent::findBorder($arr);
            }catch (\Exception $e){
                return Responses::error([],'403','请输入正确的参数');
            }
            return Responses::success($data);
        }

        $id= (integer)$request->get('id');
           //查看全部经纪人
            $data= Agent::lookBroder();
          //查看经纪人详情信息
            if (!empty($id)){
                try {
                    $redis=  Redis::createRedisObject();
                }catch (\Exception $except){
                    return Responses::error('Redis初始化错误');
                }
            $redis->select(1);
          $data=  $redis->hgetAll($id);
          if (empty($data)){
              $data= Agent::lookBroder();
              foreach ($data as $key => $value){
                  $key= array_keys($value);
                  for ($i=1;$i<count($key);$i++){
                      $redis->hset($value['id'],$key[$i],$value[$key[$i]]);
                  }
              }
              //获取redis存储中的详情信息
              $data=  $redis->hgetAll($id);
              return Responses::success($data);
          }
          return Responses::success($data);
        }
        return Responses::success($data);
          //初始化数据库信息到redis
    }

    //添加经纪人
    function broker(Request $request){
        $param = $this->getParam($request);
        $data= Agent::border($param);
        return Responses::success($data,'200','添加成功');
    }
    //修改经纪人
    function updateBroker(Request $request){
        $id=(integer) $request->get('id');
        $param = $this->getParam($request);
        $data= Agent::updateBroker($id,$param);
        return $data? Responses::success($data,'200','修改成功'):
            Responses::success($data,'403','操作失败');
    }
    //删除经纪人
    function deleteBroker(Request $request){
        $id=  (integer) $request->post('id');
        $data=Agent::deleteBroker($id);
       return $data? Responses::success($data,'200','删除成功'):
           Responses::success($data,'404','数据不存在');
    }
    /**
     * @param Request $request 请求数据获取
     */
    private function getParam(Request $request)
    {
        $name = (string)$request->post('name');
        $phone = (string)$request->post('phone');
        $company = (string)$request->post('company');
        $new_houses = (integer)$request->post('new_houses');
        $second_hand_house = (integer)$request->post('second_hand_house');
        $rent_house = (integer)$request->post('rent_house');
        $param = [
            'name' => $name,
            'phone' => $phone,
            'company' => $company,
            'new_houses' => $new_houses,
            'second_hand_house' => $second_hand_house,
            'rent_house' => $rent_house,
        ];
        try {
        $request->validate([
            'name' =>'required',
            'phone' =>'required',
            'company' =>'required',
            'new_houses' =>'required',
            'second_hand_house' =>'required',
            'rent_house' =>'required',
        ]);
        }catch (ValidationException $e){
            return Responses::error($e->errors());
        }
        return $param;
    }


}
